﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emerald;
using KaroInterface;
using Action=Emerald.Action;

namespace AITester
{
    public partial class Form1 : Form
    {
        string DllExternalAi1 { get; set; }
        string DllExternalAi2 { get; set; }

        private IArtificialIntelligence Ai1 { get; set; }
        private IArtificialIntelligence Ai2 { get; set; }

        private Game game { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void button_select_1_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = DllExternalAi1;
            
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                DllExternalAi1 = openFileDialog1.FileName;
                text_dll_1.Text = DllExternalAi1;
            }
        }

        private void button_select_2_Click(object sender, EventArgs e)
        {
            openFileDialog2.FileName = DllExternalAi2;
            if (openFileDialog2.ShowDialog(this) == DialogResult.OK)
            {
                DllExternalAi2 = openFileDialog1.FileName;
                text_dll_2.Text = DllExternalAi2;
            }
        }

        private void button_run_Click(object sender, EventArgs e)
        {
            #region set the ai's

            System.Runtime.Remoting.ObjectHandle objectHandle1, objectHandle2;
            IPlayer player1, player2;

            objectHandle1 = Activator.CreateInstanceFrom(DllExternalAi1, "Emerald.AI");
            player1 = (IPlayer) objectHandle1.Unwrap();
            player1.SColor = "Red";

            if(radioButton1.Checked)
                objectHandle2 = Activator.CreateInstanceFrom(DllExternalAi2, "Emerald.AI");
            else if(radioButton2.Checked)
                objectHandle2 = Activator.CreateInstanceFrom(DllExternalAi1, "Emerald.RandomAI");
            else
                throw new NotImplementedException("Shouldn't go here");

            player2 = (IPlayer) objectHandle2.Unwrap();
            player2.SColor = "White";

            Ai1 = (IArtificialIntelligence)player1;
            Ai2 = (IArtificialIntelligence)player2;


            #endregion

            game = new Game(Ai1, Ai2, EColor.Red);

            progressBar.Minimum = 0;
            progressBar.Maximum = int.Parse(numeric_amount_of_runs.Text);
            for (int i = 0; i < int.Parse(numeric_amount_of_runs.Text); i++)
            {
                output_text.Text="Start game #"+i+1+"\n";
                progressBar.Value = i;
                Application.DoEvents();
                while (game.GameState.IsWonByPlayer() == null)
                {
                    IArtificialIntelligence ai = (IArtificialIntelligence)game.PlayerToPlay;
                    Action a = new Action(ai.DoTurn(game.GameState.DataString));
                    game.GameState.PerformAction(a);
                    
                }

                output_text.Text = "Game #" + i + 1 + " ended, winning player = " + game.GameState.IsWonByPlayer() + "\n";
                Application.DoEvents();
            }
            progressBar.Value = 0;

        }
        
    }
}
